const Router = require("koa-router");
// const { HttpException } = require("../../../core/http-exception");
// const { PositiveIntegerValidator } = require("../../validator/validator");
const { Flow } = require("../../../models/flow");
const { Auth } = require("../../../middlewares/auth");
const { ScopeType } = require("../../../lib/enum");
const {
  PositiveIntegerValidator,
  LikeValidator,
} = require("../../validator/validator");
const { Art } = require("../../../models/art");
const { Like } = require("../../../models/like");

const router = new Router({
  prefix: "/v1/classic",
});

router.get("/latest", new Auth(ScopeType.USER).m, async (ctx, next) => {
  const flow = await Flow.findOne({
    order: [["index", "DESC"]],
  });
  const art = await Art.getData(flow.art_id, flow.type, false);
  const uaerLike = await Like.userLike(art.id, art.type, ctx.auth.uId);
  await art.setDataValue("index", flow.index);
  await art.setDataValue("userLike", uaerLike);
  ctx.body = {
    data: art,
    success: true,
  };
});

router.get("/:index/next", new Auth(ScopeType.USER).m, async (ctx) => {
  const v = await new PositiveIntegerValidator().validate(ctx, {
    id: "index",
  });
  const flow = await Flow.findOne({
    where: {
      index: v.get("path.index") + 1,
    },
  });

  if (!flow) {
    throw new global.errs.NotFound("已经是最后一期了");
  }
  const art = await Art.getData(flow.art_id, flow.type, false);
  const uaerLike = await Like.userLike(art.id, art.type, ctx.auth.uId);
  await art.setDataValue("index", flow.index);
  await art.setDataValue("userLike", uaerLike);
  art.exclude = ['index','like_status']

  ctx.body = {
    data: art,
    success: true,
  };
});

router.get("/:index/last", new Auth(ScopeType.USER).m, async (ctx) => {
  const v = await new PositiveIntegerValidator().validate(ctx, {
    id: "index",
  });
  const flow = await Flow.findOne({
    where: {
      index: v.get("path.index") - 1,
    },
  });
  if (!flow) {
    throw new global.errs.NotFound("没有之前的期刊");
  }
  const art = await Art.getData(flow.art_id, flow.type, false);
  const uaerLike = await Like.userLike(art.id, art.type, ctx.auth.uId);
  await art.setDataValue("index", flow.index);
  await art.setDataValue("userLike", uaerLike);
  ctx.body = {
    data: art,
    success: true,
  };
});

//获取点赞情况
router.get("/:type/:id/like", new Auth(ScopeType.USER).m, async (ctx, next) => {
  const v = await new LikeValidator().validate(ctx);
  const art = await Art.getData(v.get("path.id"), v.get("path.type"));

  ctx.body = {
    data: art,
    success: 200,
  };
});

router.get("/myLike", new Auth(ScopeType.USER).m, async (ctx) => {
  const arts = await Like.getMyLikeClassic(ctx.auth.uId);
  ctx.body = {
    arts,
  };
});

router.get("/:type/:id", new Auth().m, async (ctx) => {
  const v = await new LikeValidator().validate(ctx);
  const id = v.get("path.id");
  const type = v.get("path.type");
  const art = await new Art(id, type).getDetail(ctx.auth.uId);
  ctx.body = {art};
});

module.exports = router;
